﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using XcoAppSpaces.Contracts.Transport;
using XcoAppSpaces.Serialization.Json;
using XcoAppSpaces.Transport.Sockets;

namespace test.XcoAppSpaces.Transport.Sockets
{
	[TestClass]
	public class TimeoutTests
	{
		[TestMethod]
		public void TestPerformance()
		{
			//XcoLoggerFactory.CreateInstance = (t => new TestLogger.TestLogger());

			XcoTCPTransportService comm1 = new XcoTCPTransportService(8003);
			XcoJsonSerializer ser1 = new XcoJsonSerializer();
			XcoTCPTransportService comm2 = new XcoTCPTransportService(8004);
			XcoJsonSerializer ser2 = new XcoJsonSerializer();
			ReceivedTmp received = new ReceivedTmp();
			try
			{
				comm1.OnMessageReceived += received.MessageReceived;
				comm2.OnMessageReceived += received.MessageReceived;
				
				comm1.Start();
				comm2.Start();
				string addr2 = Dns.GetHostName().ToLower() + ":" + 8003;


				received.StopWatch.Start();

				byte[] bytes = ser1.Serialize(new IntMsg { value = 1, strValue1 = "123", strValue2 = "test", msgPartList = new List<MsgPart> { new MsgPart { exValue = new Exception(), strValue = "bla" } } });

				comm1.Send(new XcoMessage { Content = bytes, CausalityContext = null }, addr2);


				received.StopWatch.Stop();

				//Thread.Sleep(3 * 60 * 1000);
				Thread.Sleep(3);

				Assert.AreEqual(1, received.Counter);

			}
			catch (Exception ex)
			{
				Console.WriteLine("ERROR: " + ex);

			}
			finally
			{
				comm1.Stop();
				comm2.Stop();
			}
		}
	}
}
